-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(types): defineComponent()
with generics support
#7963
Conversation
BREAKING CHANGE: The type of `defineComponent()` when passing in a function has changed. This overload signature was rarely used in the past, so it is changed to something more useful. Previously the return type was `DefineComponent`, now it is a function type that inherits the generics of the function passed in. The function passed in as the first argument now also requires a return type of a render function, as the signature is no longer meant to be used for other use cases.
931bef8
to
6ca4b80
Compare
BREAKING CHANGE: The type of `defineComponent()` when passing in a function has changed. This overload signature is rarely used in practice and the breakage will be minimal, so repurposing it to something more useful should be worth it. close vuejs#3102
is there a example about custom event generic support |
Currently we cannot use generic + typed emits or slots (in other words only |
I'm wondering why this issue was not mentioned in the documentation. Or rather, should this situation be included in the document to prevent others from having to spend time like I did to confirm the problem and search for issues? |
If the second parameter is given, the generic type does not work. |
Breaking Changes
The type of
defineComponent()
when passing in a function has changed. This overload signature is rarely used in practice and the breakage will be minimal, so repurposing it to something more useful should be worth it.Previously the return type was
DefineComponent
, now it is a function type that inherits the generics of the function passed in. The function passed in as the first argument now also requires a return type of a render function, as the signature is no longer meant to be used for other use cases.Another side effect is that a very specific case of mixins usage breaks:
Note the breakage only happens when calling
defineComponent()
inline in themixins
option on a separately declared plain object. This should be extremely unlikely in userland code because if one wants typed mixins, one will certainly define it like this to get type inference for the mixin itself:This continues to work, so I believe this issue is negligible in practice. It would be nice to fix, but I couldn't figure it out.
Generics Example
Extra Options
This signature is expected to only be used with Composition API. Misc options can be passed via the second argument:
Only
name
,inheritAttrs
,props
andemits
are allowed.Runtime Props
The final component still need runtime props declared. This can be done automatically via a babel plugin, or manually like so:
The type definition ensures that the manual type and the runtime options must match.
Emits Validation
Type inference based on runtime
emits
option is supported, including the matchingonXXX
prop on the resulting props interface.